package com.zpb.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.dto.CommentDto;
import com.zpb.framework.domain.entity.Comment;
import com.zpb.framework.domain.enums.ResultCodeEnum;
import com.zpb.framework.domain.vo.PageVo;
import com.zpb.framework.exception.SystemException;
import com.zpb.framework.service.CommentService;
import com.zpb.framework.utils.DictCodeUtil;
import com.zpb.framework.utils.JwtUtil;
import com.zpb.framework.utils.SecurityUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/12
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping("/commentList")
    public ApiResult<PageVo> commentList(@RequestParam(required = false) Long articleId,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        if (articleId == null) {
            throw new SystemException(ResultCodeEnum.MISS_PARAM.getCode(), "文章id不能为空");
        }
        return commentService.getCommentList(DictCodeUtil.IS_ARTICLE_COMMENT, articleId, pageNum, pageSize);
    }

    @PostMapping
    public ApiResult addComment(@RequestBody CommentDto commentDto) {
        if (commentDto == null) {
            throw new SystemException(ResultCodeEnum.MISS_PARAM.getCode(), "请求参数不存在");
        }
        if (commentDto.getContent() == null || commentDto.getContent().trim().isEmpty()) {
            throw new SystemException(ResultCodeEnum.MISS_PARAM.getCode(), "评论内容不能为空");
        }
        return commentService.addComment(commentDto);

    }

    @GetMapping("/linkCommentList")
    public ApiResult<PageVo> linkCommentList(@RequestParam(required = false) Long articleId,
                                             @RequestParam(defaultValue = "1") Integer pageNum,
                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        articleId = articleId == null ? 1L : articleId;
        return commentService.getCommentList(DictCodeUtil.IS_LINK_COMMENT, articleId, pageNum, pageSize);
    }

}
